//
//  UIBarButtonItem+Extension.swift
//  WeiBo
//
//  Created by 许世强 on 2017/3/27.
//  Copyright © 2017年 许世强. All rights reserved.
//

import UIKit

//给UIBarButtonItem扩展遍构造函数
extension UIBarButtonItem {

    //无需返回nil,这里又不需要进行判断  Any好比OC里的id  Selector类型
    convenience init(title:String ,imageName:String? = nil, target:Any? ,action:Selector) {
        
        //便利构造函数没有构造对象的能力,需要调用默认的构造函数
        self.init()
        
        let button = UIButton()
        
        button.setTitle(title, for: .normal)
        
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        //给button添加点击事件
        button.addTarget(target, action: action, for: .touchUpInside)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        if let imageName = imageName {
            button.setImage(UIImage(named:imageName), for: .normal)
        }
        
        button.sizeToFit()
        //关联自定义的button按钮
        self.customView = button
    }
}
